var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var wxParse = require('../../lib/wxParse/wxParse.js');
var app = getApp();

Page({
  data: {
    tourList: [],
    banner: [],
    city: "成都",
    category:[],
    pageSize:10,
    pageNum:1
  },
  getData: function () {
    console.log(api.SceneIndex)
    let that = this;
    util.request(api.TourIndex,{pageSize:that.data.pageSize,pageNum:that.data.pageNum}).then(function (res) {
      console.log(res)
      if (res.code === '200') {
        that.setData({
          tourList: res.datas.tourList,
          banner: res.datas.banner,
          category: res.datas.category
        });
        //  wxParse.wxParse('article', 'html', article, that, 5);
      }
    });
  },

  onLoad: function (options) {
    app.init(options, this);
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面初始化 options为页面跳转所带来的参数
    this.getData();
    app.getShareInfo(this);
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  selectCity: function () {
    wx.navigateTo({
      url: '/pages/city/index'
    })
  },
  fnTourList: function (e) {
    var that = this;
    var typeId = e.currentTarget.dataset.item.id;
    var typeName = e.currentTarget.dataset.item.name;
    var url = "/pages/toursearch/toursearch?typeId=" + typeId + "&typeName=" + typeName;
    console.log(url)
    wx.navigateTo({
      url: url,
    })
  },
  fnToIndex: function () {
    wx.switchTab({
      url: '/pages/index/index-new',
    })
  },
  fnGetMore:function () {
    var that =this;
    var pageSize = that.data.pageSize;
    var pageNum = that.data.pageNum;
    var data={
      pageSize:pageSize,
      pageNum:pageNum+1
    }
    util.request(api.TourMore,data).then(function(res){
      if(res.code=="200"){
        if (res.datas && res.datas.length == that.data.pageSize) {
          //数据够显示一页
          that.setData({
            tourList: that.data.tourList.concat(res.datas),
            pageNum: that.data.pageNum + 1,
            noMore: false
          })
        } else if (res.datas && res.datas.length > 0) {
          //数据不够够显示一页
          that.setData({
            tourList: that.data.tourList.concat(res.datas),
            pageNum: that.data.pageNum + 1,
            noMore: true
          })
        } else {
          wx.showToast({
            title: '没有更多了',
            image: "/static/images/icon_error.png",
            duration: 2000
          })
          that.setData({
            noMore: true
          })
        }
      }
    });
  },
  fnShare: function (e) {
    var that = this;
    var path = "";
    var type = e.currentTarget.dataset.type;
    var goodsId = e.currentTarget.dataset.id;
    var imgUrl = e.currentTarget.dataset.url;
    var goodsName = e.currentTarget.dataset.name;
    var address = e.currentTarget.dataset.address;
    var price = e.currentTarget.dataset.price;
    var marketPrice = e.currentTarget.dataset.marketprice;
    if (type == 'hotel') {
      path = "/pages/hotelDetail/hoteldetail";
    } else if (type == "scene") {
      path = "/pages/sceneDetail/sceninfo";
    } else {
      path = "/pages/tourDetail/travelline"
    }
    app.fnShareGoods(path, goodsId, imgUrl, goodsName,type,address,price,marketPrice);
  }

})